/*
  Copyright 1996-2003
  Simon Whiteside

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  $Id: Messages.java,v 1.3 2003/02/24 18:10:53 simkin_cvs Exp $
*/
package simkin;

/**
 * This class holds the strings used by the interpreter for runtime errors.
 */
final class Messages {
    static final String CouldNotCreateIterator="Object could not create an iterator, in a foreach statement";
    static final String CannotApplyForEach="Cannot apply foreach to a non-executable object";
    static final String EndNotGreaterThanStart="End value is not greater than the start value in a for statement";
    static final String StartNotGreaterThanEnd="Start value is not greater than the end value in a for statement";
    static final String ZeroStep="Cannot use a zero step increment in a for statement";
    static final String FieldNotFound="Field \"$1\" not found";
    static final String NoAttributesOnArrays="Attributes are not supported on Java arrays";
    static final String ArrayIndexNotOnExecutable="Array index only supported on Executable objects or Java arrays";
    static final String AttributeNotFound="Attribute \"$1\" not found";
    static final String AttributeNotOnExecutable="Attributes not supported on non-Executable objects";
    static final String CouldNotSetAttributeOnClass="Could not set attribute \"$1\" on class \"$2\"";
    static final String CouldNotSetFieldOnClass="Could not set field \"$1:$2\" on class \"$3\"";
    static final String CouldNotCallMethodClass="Could not call method \"$1\" on class \"$2\"";
    static final String DivideByZero="Divide by zero error";
    static final String CouldNotFindMethod="Could not find method \"$1\", is it declared public?";
    static final String CouldNotCallMethod="Could not call method \"$1\", is it declared public?";
    static final String CouldNotAccessField="Could not access field \"$1\", is it declared public?";
    static final String CouldNotReadField="Could not read field \"$1\" value, is it declared public?";
    static final String CouldNotSetField="Could not set field value \"$1\", is it declared public?";
    /**
     * This method drops the argument into the place of "$1" in the given message string
     * @param message the message containing an $1
     * @param arg the value to replace the $1 with
     */
    static public String formatMessage(String message,String arg){
	return replaceTag(message,"$1",arg);
    }
    /**
     * This message drops the arguments into the place of "$1" and "$2" in the given message string
     * @param message the message containing a $1 and $2
     * @param arg1 the value to replace the $1 with
     * @param arg2 the value to replace the $2 with
     */
    static public String formatMessage(String message,String arg1,String arg2){
	return formatMessage(replaceTag(message,"$2",arg2),arg1);
    }
    /**
     * This method drops the arguments into the place of "$1", "$2" and "$3" in the given message string
     * @param message the message containing a $1, $2 and $3
     * @param arg1 the value to replace the $1 with
     * @param arg2 the value to replace the $2 with
     * @param arg3 the value to replace the $3 with
     */
    static public String formatMessage(String message,String arg1,String arg2,String arg3){
	return formatMessage(replaceTag(message,"$3",arg3),arg1,arg2);
    }
    /**
     * This method drops the arguments into the place of "$1", "$2", "$3" and "$4" in the given message string
     * @param message the message containing a $1, $2 $3 and $4
     * @param arg1 the value to replace the $1 with
     * @param arg2 the value to replace the $2 with
     * @param arg3 the value to replace the $3 with
     * @param arg4 the value to replace the $4 with
     */
    static public String formatMessage(String message,String arg1,String arg2,String arg3,String arg4){
	return formatMessage(replaceTag(message,"$4",arg4),arg1,arg2,arg3);
    }
    /**
     * This method replaces all instances of a substring in a larger string with a new substring.
     * @param original - the string to be replaced
     * @param old_value - the value to be replaced
     * @param new_value - the  new value
     * @return the original string with the old value replaced by the new
     */
    static public String replaceTag(String original,String old_value,String new_value){
	String replaced=original;
	int index=original.indexOf(old_value);
	if (index>=0){
	    int pos=0;
	    StringBuffer buffer=new StringBuffer(original.length());
	    while (index>=0){
		buffer.append(original.substring(pos,index));
		buffer.append(new_value);
		pos=index+old_value.length();
		index=original.indexOf(old_value,pos);
	    }
	    if (pos<original.length())
		buffer.append(original.substring(pos));
	    replaced=buffer.toString();
	}
	return replaced;
    }
}
